﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using NetDiskPRJ.DBUtility;

namespace NetDiskPRJ.DAL
{
    public class InfoUpdateFieldLog
    {
        public InfoUpdateFieldLog()
        {
 
        }

        
		/// <summary>
		/// 增加一条数据
		/// </summary>
        public void Add(List<Model.InfoUpdateFieldLog> modelList)
        {
            StringBuilder strSql = new StringBuilder();
            CommandInfo cmds = new CommandInfo();
            List<CommandInfo> cmdList = new List<CommandInfo>();
            foreach (Model.InfoUpdateFieldLog model in modelList)
            {
                strSql = new StringBuilder();
                cmds = new CommandInfo();
                strSql.Append("insert into t_InfoUpdateFieldLog(");
                strSql.Append("FFieldName,FUpdateDate,FOldContent,FNewContent,FInfoSourceID,FUpdLogID,FUpdUserName,FTableName)");
                strSql.Append(" values (");
                strSql.Append("@FFieldName,@FUpdateDate,@FOldContent,@FNewContent,@FInfoSourceID,@FUpdLogID,@FUpdUserName,@FTableName)");
                strSql.Append(";select @@IDENTITY");
                SqlParameter[] parameters = {
					new SqlParameter("@FFieldName", SqlDbType.VarChar,60),
					new SqlParameter("@FUpdateDate", SqlDbType.DateTime),
					new SqlParameter("@FOldContent", SqlDbType.NVarChar,255),
					new SqlParameter("@FNewContent", SqlDbType.NVarChar,255),
					new SqlParameter("@FInfoSourceID", SqlDbType.NVarChar,50),
					new SqlParameter("@FUpdLogID", SqlDbType.NVarChar,30),
					new SqlParameter("@FUpdUserName", SqlDbType.NVarChar,30),
					new SqlParameter("@FTableName", SqlDbType.VarChar,255)};
                parameters[0].Value = model.FFieldName;
                parameters[1].Value = model.FUpdateDate;
                parameters[2].Value = model.FOldContent;
                parameters[3].Value = model.FNewContent;
                parameters[4].Value = model.FInfoSourceID;
                parameters[5].Value = model.FUpdLogID;
                parameters[6].Value = model.FUpdUserName;
                parameters[7].Value = model.FTableName;
                cmds.CommandText = strSql.ToString();
                cmds.Parameters = parameters;
                cmdList.Add(cmds);
            }

            if (cmdList.Count > 0)
            {
                DbHelperSQL.ExecuteSqlTranWithIndentity(cmdList);
            }
        }

        /// <summary>
        /// 根据条件获取信息修改记录
        /// <creator>sunlj</creator>
        /// <date>2013-3-21</date>
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public List<Model.InfoUpdateFieldLog> GetModelList(string strWhere)
        {
            List<Model.InfoUpdateFieldLog> InfoUpdateFieldLogObjects = new List<Model.InfoUpdateFieldLog>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM t_InfoUpdateFieldLog ");
            if (strWhere != "")
            {
                strSql.Append(" WHERE "+strWhere);
            }
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach(DataRow row in ds.Tables[0].Rows)
                {
                    Model.InfoUpdateFieldLog model = new Model.InfoUpdateFieldLog();
                    model = DataRowToModel(row);
                    InfoUpdateFieldLogObjects.Add(model);
                }
            }
            return InfoUpdateFieldLogObjects;
        }
        /// <summary>
        /// 根据条件获取项目信息修改记录
        /// <creator>wangl</creator>
        /// <date>2013-03-25</date>
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public List<Model.InfoUpdateFieldLog> GetModelListByProject(string projectID)
        {
            List<Model.InfoUpdateFieldLog> InfoUpdateFieldLogObjects = new List<Model.InfoUpdateFieldLog>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT FUpdateID,FFieldName,FUpdateDate,FOldContent,FNewContent,FInfoSourceID,FUpdLogID,FUpdUserName,FTableName FROM t_InfoUpdateFieldLog ");
            strSql.Append(" WHERE FInfoSourceID=@FInfoSourceID AND FTableName='t_Project'");
            SqlParameter[] parameters = {
					new SqlParameter("@FInfoSourceID", SqlDbType.NVarChar,50)
                                        };
            parameters[0].Value = projectID;
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    Model.InfoUpdateFieldLog model = new Model.InfoUpdateFieldLog();
                    model = DataRowToModel(row);
                    InfoUpdateFieldLogObjects.Add(model);
                }
            }
            return InfoUpdateFieldLogObjects;
        }
        /// <summary>
        /// 得到一个对象实体
        /// <creator>sunlj</creator>
        /// <date>2013-3-21</date>
        /// </summary>
        public NetDiskPRJ.Model.InfoUpdateFieldLog DataRowToModel(DataRow row)
        {
            NetDiskPRJ.Model.InfoUpdateFieldLog model = new NetDiskPRJ.Model.InfoUpdateFieldLog();
            if (row != null)
            {
                if (row["FUpdateID"] != null && row["FUpdateID"].ToString() != "")
                {
                    model.FUpdateID = decimal.Parse(row["FUpdateID"].ToString());
                }
                if (row["FFieldName"] != null)
                {
                    model.FFieldName = row["FFieldName"].ToString();
                }
                if (row["FUpdateDate"] != null && row["FUpdateDate"].ToString() != "")
                {
                    model.FUpdateDate = DateTime.Parse(row["FUpdateDate"].ToString());
                }
                if (row["FOldContent"] != null)
                {
                    model.FOldContent = row["FOldContent"].ToString();
                }
                if (row["FNewContent"] != null)
                {
                    model.FNewContent = row["FNewContent"].ToString();
                }
                if (row["FInfoSourceID"] != null)
                {
                    model.FInfoSourceID = row["FInfoSourceID"].ToString();
                }
                if (row["FUpdLogID"] != null)
                {
                    model.FUpdLogID = row["FUpdLogID"].ToString();
                }
                if (row["FUpdUserName"] != null)
                {
                    model.FUpdUserName = row["FUpdUserName"].ToString();
                }
                if (row["FTableName"] != null)
                {
                    model.FTableName = row["FTableName"].ToString();
                }
            }
            return model;
        }


    }
}
